home *** CD-ROM | disk | FTP | other *** search
- /*
- Present a dialog box with a text rectangle, a scroll bar, and an OK
- button. This can be used as a help dialog. Pass in a dialog
- resource number and a handle to the text. The dialog should
- have the OK button as the first item, and user items for the
- second and third items. The second is the rect in which the
- control is drawn, the third is the rect in which text is drawn.
- Only the OK button should be enabled.
-
- The text handle should be locked before calling TextDlog and
- unlocked after.
- */
-
- # include <EventMgr.h>
- # include <DialogMgr.h>
- # include <ControlMgr.h>
-
- # define nil 0L
-
- enum /* item numbers */
- {
- okBut = 1,
- scrollBarUser,
- textRectUser
- };
-
- static ControlHandle theScroll;
- static Boolean needScroll; /* true if need scroll bar */
- static int curLine;
- static int halfPage;
- static TEHandle teHand;
-
-
- /*
- Proc for drawing scroll bar user item.
- */
-
- static pascal void DrawScroll (theDialog, itemNo)
- DialogPtr theDialog;
- int itemNo;
- {
- if (needScroll)
- ShowControl (theScroll);
- }
-
-
- /*
- Proc for drawing text display user item.
- */
-
- static pascal void DrawTextRect (theDialog, itemNo)
- DialogPtr theDialog;
- int itemNo;
- {
- Rect r;
- int itemType;
- Handle itemHandle;
-
- GetDItem (theDialog, textRectUser, &itemType, &itemHandle, &r);
- if (needScroll)
- FrameRect (&r);
- r = (**teHand).viewRect;
- TEUpdate (&r, teHand);
- }
-
-
- /*
- Scroll to the correct position. lDelta is the
- amount to CHANGE the current scroll setting by.
- */
-
- static DoScroll (lDelta)
- int lDelta;
- {
- int newLine;
-
- newLine = curLine + lDelta;
- if (newLine < 0) newLine = 0;
- if (newLine > GetCtlMax (theScroll)) newLine = GetCtlMax (theScroll);
- SetCtlValue (theScroll, newLine);
- lDelta = (curLine - newLine ) * (**teHand).lineHeight;
- TEScroll (0, lDelta, teHand);
- curLine = newLine;
- }
-
-
- static pascal void __TrackScroll (theScroll, partCode)
- ControlHandle theScroll;
- int partCode;
- {
- int lDelta;
-
- if (partCode == GetCRefCon (theScroll)) /* still in same part? */
- {
- switch (partCode)
- {
- case inUpButton: lDelta = -1; break;
- case inDownButton: lDelta = 1; break;
- case inPageUp: lDelta = -halfPage; break;
- case inPageDown: lDelta = halfPage; break;
- }
- DoScroll (lDelta);
- }
- }
-
-
- /*
- Filter to handle hits in scroll bar
- */
-
- static pascal Boolean TextFilter (theDialog, theEvent, itemHit)
- DialogPtr theDialog;
- EventRecord *theEvent;
- int *itemHit;
- {
- Point thePoint;
- int thePart;
-
- if (theEvent->what == mouseDown)
- {
- thePoint = theEvent->where;
- GlobalToLocal (&thePoint);
- thePart = TestControl (theScroll, thePoint);
- if (thePart == inThumb)
- {
- (void) TrackControl (theScroll, thePoint, nil);
- DoScroll (GetCtlValue (theScroll) - curLine);
- }
- else if (thePart != 0)
- {
- SetCRefCon (theScroll, (long) thePart);
- (void) TrackControl (theScroll, thePoint, __TrackScroll);
- }
- }
- return (false);
- }
-
-
- TextDialog (dlogNum, textHandle, fontNum, fontSize, wrap)
- int dlogNum;
- Handle textHandle;
- int fontNum;
- int fontSize;
- Boolean wrap;
- {
- GrafPtr oldPort;
- DialogPtr theDialog;
- int itemNo;
- int itemType;
- Handle itemHandle;
- Rect r;
- int scrollLines;
- int viewLines;
- Handle oldHText;
- ProcPtr filterProc = nil;
-
- GetPort (&oldPort);
- theDialog = GetNewDialog (dlogNum, nil, -1L);
- GetDItem (theDialog, textRectUser, &itemType, &itemHandle, &r);
- SetDItem (theDialog, textRectUser, itemType, DrawTextRect, &r);
- /*
- incorporate text into a TERec.
- */
- SetPort (theDialog);
- TextFont (fontNum);
- TextSize (fontSize);
- InsetRect (&r, 4, 4);
- teHand = TENew (&r, &r);
- if (!wrap)
- (**teHand).crOnly = -1;
- oldHText = (**teHand).hText; /* save this. restore later */
- (**teHand).hText = textHandle;
- TECalText (teHand);
- viewLines = (r.bottom - r.top) / (**teHand).lineHeight;
- scrollLines = (**teHand).nLines - viewLines;
- needScroll = (scrollLines > 0);
- GetDItem (theDialog, scrollBarUser, &itemType, &itemHandle, &r);
- SetDItem (theDialog, scrollBarUser, itemType, DrawScroll, &r);
- if (needScroll)
- {
- theScroll = NewControl (theDialog, &r, "\p", false, 0, 0, 0,
- scrollBarProc, nil);
- SetCtlMax (theScroll, scrollLines);
- halfPage = viewLines / 2;
- curLine = 0;
- filterProc = (ProcPtr) TextFilter;
- }
-
- ShowWindow (theDialog);
- /*do {*/
- ModalDialog (filterProc, &itemNo);
- /*} while (itemNo != okBut);*/
-
- /*
- restore hText field of TE record before disposing of it.
- */
- (**teHand).hText = oldHText;
- TEDispose (teHand);
- /*ReleaseResource (textHandle);*/
- if (needScroll)
- DisposeControl (theScroll);
- SetPort (oldPort);
- DisposDialog(theDialog);
- }
-